'ui';
var file_paths = null;
var project_root = '/storage/emulated/0/Pictures/农房照片/';
var paths = Array();
var file_name = Array();
var zip_file = null;

function main(){

    var arr = files.listDir("/storage/emulated/0/Pictures/");
    arr.forEach(element => {
    if (element.indexOf(".jpg")!=-1){
        paths.push(element);
        log("检测到文件:" + element);
        var new_str = element.slice(0,-17);
        try{
            if (new_str!=null){
                file_name.push(new_str);
                }
        }
        catch(error){
                console.log("错误为:" + error);
            }
        }
    });
   
    
    
    paths.forEach(x=> 
            {   
                
                if (x!='' && x!=null){
                var new_str = x.slice(0,-17);
                try{
                if (new_str!=null){
                    log("分组名称:" + new_str);
        
                var folder = files.create("/storage/emulated/0/Pictures/"+new_str+"/");
                if (folder) log("已创建文件夹");
                else {log("文件夹已创建！")};
                    
                var result = files.move("/storage/emulated/0/Pictures/"+x,"/storage/emulated/0/Pictures/"+new_str+"/"+x);
                if (result){log("分组成功!")};
                    }
                }
                catch(error){
                    console.log("出错！");
                    }
                }
            }
        );
    if (paths.length==0){
        toastLog("没有需要处理的文件!");
    }
    move_to();
}

function move_to(){
    file_name.forEach(function(x){
        if (!$files.exists(project_root)) {
            // 文件夹不存在 创建文件夹 
            $files.create(project_root);
            log('农房路径创建成功！'); 
        }
        var move_r = files.move("/storage/emulated/0/Pictures/"+x,project_root + x+'/');
        if (move_r){
                toastLog("文件已分类归总!");
            }
    });
}


function zip(dir,file_name){

    // 需压缩文件路径
    //let dir = "/storage/emulated/0/Pictures/";
    
    //压缩后的文件
    var zipfile = $files.join(dir,file_name +".zip");
    
    // 删除同名文件
    $files.remove(zipfile);
    
    // 压缩
    $zip.zipDir(dir,zipfile);
    toastLog("压缩完成,压缩包路径:" + zipfile);
    
    }


function send_mail(tomail,path){
    app.sendEmail({
        email: [tomail],
        subject: "文件请接收",
        text: "荥阳市农村房屋不动产调查",
        attachment:'file://' + path
    });
}

ui.layout(
    <frame  w="*" h="*" bg="#000000">
        <img src="https://pic1.zhimg.com/80/v2-d5011c32ec651f7b3c63c26a69801e09_720w.jpg?source=1940ef5c" scaleType="fitXY"/>
        <button id="startRun" layout_gravity="center" marginBottom="60" w="auto" h="auto" text="开始分组" textSize="30sp"/> 
        <button id="zipRun" layout_gravity="center" w="auto" h="auto" text="压缩打包" textSize="30sp"/>
        <button id="other" layout_gravity="center" marginTop="60" w="auto" h="auto" text="联系作者" textSize="30sp"/>
        <button id="view_file" layout_gravity="center" marginTop="120" w="auto" h="auto" text="发送文件" textSize="30sp"/>

    </frame>
);

ui.startRun.click(
    ()=>{
        threads.start(function(){
            //在新线程执行的代码
            file_paths = main();
        });
    }
);

ui.zipRun.click(()=>{

    new java.lang.Thread(function() {
        if (files.exists(project_root)){
            var file_name = rawInput("请输入压缩文件名:","如:吴沟村");

            zip_file = file_name+".zip";
        if (file_name){
            toast("开始打包,过程一般不超过5分钟...");
            zip(project_root,file_name);
            
            //此处调用压缩函数！
            }
        }
        else{
            toast("请先运行分组!");
        }
       }).start();
});

ui.other.click(
    function(){
    var qq = "2528104776";
    app.startActivity({ 
    action: "android.intent.action.VIEW", 
    data:"mqq://im/chat?chat_type=wpa&version=1&src_type=web&uin=" + qq, 
    packageName: "com.tencent.mobileqq", 
        });
    }
);

ui.view_file.click(()=>{

    new java.lang.Thread(function() {
        var scan_file = files.listDir(project_root, function(name){
            return name.endsWith(".zip") && files.isFile(files.join(project_root, name));
                        });
        scan_file.forEach(function(x){
            if(x==zip_file || x!=null){
                var mail = dialogs.prompt("请输入接收的邮箱地址","2528104776@qq.com");
                log("/storage/emulated/0/Pictures/"+scan_file[0]+".zip");
                send_mail(mail,project_root+scan_file[0]);
                }
            else{
                toast("文件不存在!");
                }
            });
        }).start();
    });
    
